package com.syher.seconds.kill.provider.core.configure;

import com.syher.seconds.kill.provider.core.kafka.JsonSerializer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class KafkaConfig {

    @Value("${kafka.brokerList}")
    private String brokerList;

    @Value("${kafka.switch}")
    private boolean check;

    @Bean
    public KafkaProducer<String, Object> build() {
        if (!check) {
            return null;
        }

        Map<String, Object> props = new HashMap<String, Object>(16);
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        props.put("metadata.broker.list", brokerList);
        props.put("bootstrap.servers", brokerList);
        props.put("key.serializer", StringSerializer.class);
        props.put("value.serializer", JsonSerializer.class);
        KafkaProducer<String, Object> producer = new KafkaProducer(props);
        return producer;
    }
}
